library(fixest)
library(modelsummary)
library(spmoran)
library(REndo)
library(WeightIt)
library(DirectEffects)
library(vtable)
library(spdep)
library(panelView)
library(ggplot2)
library(ggpubr)
library(viridis)
library(patchwork)
library(cragg)
library(mediation)
library(sensemakr)
library(robomit)
library(spatialreg)
library(rdrobust)

options(scipen = 999) ##scientific notation (no)

data <- readRDS("data_final_merged.rds")  %>% dplyr::select(NUTS_ID, Judet, Year, Month, p_ottoman, share, count, ps_1699, lon, lat, tavg, lograin, logelev, logslope, wheat_rainfed, log_large_rivers_distance,log_sea_lines_distance,route_density_2, vienna_distance, istanbul_distance,geometry)

model1 <- feols(share ~ 1| p_ottoman ~route_density_2 + wheat_rainfed + istanbul_distance + vienna_distance, data = data,  vcov = "conley")
model2 <- feols(share ~  count|p_ottoman ~ route_density_2 + wheat_rainfed + istanbul_distance + vienna_distance, data = data,  vcov = "conley")
model3 <- feols(share ~  count| Year + Month|p_ottoman ~ route_density_2 + wheat_rainfed + istanbul_distance + vienna_distance, data = data,  vcov = "conley")
model4 <- feols(share ~  count + lat + lon + lat*lon | Year + Month|p_ottoman ~ route_density_2 + wheat_rainfed + istanbul_distance + vienna_distance, data = data,  vcov = "conley")
model5 <- feols(share ~  count + ps_1699 + tavg + logelev + logslope + lograin + log_large_rivers_distance + log_sea_lines_distance| Year + Month|p_ottoman ~ route_density_2 + wheat_rainfed + istanbul_distance + vienna_distance,  data = data,  vcov = "conley")

msummary(list(model1, model2, model3,model4,model5), stars = c('.' = .1,'*' = .05, '**' = .01, '***' = .001), fmt = "%.5f")

fitstat(model1, "ivf")
fitstat(model2, "ivf")
fitstat(model3, "ivf")
fitstat(model4, "ivf")
fitstat(model5, "ivf")

summary(model1)
summary(model2)
summary(model3)
summary(model4)
summary(model5)


